/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ContentManagers.Servlets;

import ContentManagers.RoleManagers.CustomerSessionManager;
import ContentManagers.Security.Validator;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Martin San Diego
 */
public class ServletEditCart extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
                HttpSession session = request.getSession();
                int cartIndex = Integer.parseInt(request.getParameter("cartIndex"));
               
                
                if(request.getParameter("submitEdit").equals("Delete") == true || Validator.isInteger(request.getParameter("quantity")) == -1|| request.getParameter("quantity").equals("0") == true )
                {
                    Object cartObject = session.getAttribute("cartArray");
                    String[][] userCart = (String[][])cartObject;
                    userCart[0][cartIndex] = null;
                    userCart[1][cartIndex] = null;
                    
                    userCart = reArrangeArray(userCart, cartIndex);
                    
                    session.setAttribute("cartArray", userCart);
                }
        
                else if (request.getParameter("submitEdit").equals("Edit") == true)
                {
                    Object cartObject = session.getAttribute("cartArray");
                    String[][] userCart = (String[][])cartObject;
                    userCart[1][cartIndex] = request.getParameter("quantity");
                    session.setAttribute("cartArray", userCart);
                }
        
                response.sendRedirect("user_cart.jsp");
        
            } catch(Exception e) { 
                HttpSession session = request.getSession();
                session.setAttribute("generalError", "Session has timed out.");
                response.sendRedirect("index.jsp"); 
            }
    }
    public String[][] reArrangeArray(String[][] userCart, int deletedIndex)
        {
            while(deletedIndex < 9 && userCart[0][deletedIndex+1] != null && userCart[1][deletedIndex+1] != null)
            {
                userCart[0][deletedIndex] = userCart[0][deletedIndex + 1];
                userCart[1][deletedIndex] = userCart[1][deletedIndex + 1];
                deletedIndex++;
            }
            userCart[0][deletedIndex] = null;
            userCart[1][deletedIndex] = null;
            
            return userCart;
        }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
